<?php
namespace App\Logic\calculate;

use Illuminate\Support\Facades\Cache;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;

class Mining
{
    public static function index()
    {
        //挖自己的，发大家的
        /**
         * 获取单T应发
         *
         *  获取每个人的容量
         *  发放单个人的到矿机收益记录里面
         *
         *  获取总容量
         *  获取每个等级人数
         *  按奖励给每个等级的人奖励
         */
        $per = 100;

        $time = time();
        $list = DB::table('miner_host as mh')
            ->leftJoin('miner_info as mi','mi.mid','=','mh.mid')
            ->leftJoin('miner_user as mu','mu.mid','=','mh.mid')
            ->leftJoin('user as u','u.username','=','mu.user_name')
            ->where('mh.token','FIL')
            ->where('mh.start_time','<',$time)
            ->where('mh.end_time','>',$time)
            ->select('mh.id','mh.mid','mh.hspace','mh.note','mu.user_name','u.id as uid','u.level','u.rebate','u.username','u.phone')
            ->get()->toArray();

        if(!$list){
            return e_msg(400001,'数据为空');
        }
        $hspace_arr = array_column($list,'hspace');
        $sum = array_sum($hspace_arr);

        //个人单T应发
        $num = $per * 0.8 * 0.7;

        $miner_insert_arr = [];
        $user_insert_arr = [];
        $src_insert_arr = [];

        $config_sys = Cache::get('config_sys');
        foreach($config_sys as $val){
            if($val->key == 'miner_fee'){
                $miner_fee = $val->val;
            }
        }

        $src_user_name_arr = [];
        foreach($list as $k => $v) {

            $jine = $num * $v->hspace;
            $srt = $miner_fee * $v->hspace;

            if($srt <= $v->rebate && $srt > 0){
                $miner_insert_arr[] = [
                    'token' => 'FIL',
                    'mid' => $v->mid,
                    'amount' => $jine,
                    'ticket' => 'data_Mining_' . date('YmdHis'),
                    'type' => 'mining',
                    'type_note' => '挖矿',
                    'state' => 1,
                ];

                $src_insert_arr[] = [
                    'out_account' => $v->username,
                    'out_phone' => $v->phone,
                    'shift_account' => '系统',
                    'shift_phone' => '系统',
                    'amount' => -$srt,
                    'type_note' => '算力托管费',
                    'type' => 'consumption_fee',
                ];
                $src_user_name_arr[] = $v->username;
                DB::table('user')->where('username',$v->username)->decrement('rebate',$srt);

                if($v->note == 1){
                    DB::table('miner_host')->where('id',$v->id)->update(['note'=>0]);
                }

            }else{
                if($v->note == 0){
                    DB::table('miner_host')->where('id',$v->id)->update(['note'=>1]);
                }
            }


        }
        if(count($miner_insert_arr)){
            Db::table('miner_balance')->insert($miner_insert_arr);
        }
        if(count($src_insert_arr)>0){
            DB::table('record_srt')->insert($src_insert_arr);
        }

        //$base = $per * 0.8 * 0.25 * $sum;
        $base = $per * 0.8 *  $sum;

        $user_count = Db::table('user')
            ->where('state','1')
            ->where('level','>',0)
            ->select('level',DB::raw('count(*) as count'))
            ->groupBy('level')
            ->get()->toArray();
        foreach($user_count as $user_count_val) {

            $user = Db::table('user')
                ->where('state','1')
                ->where('level',$user_count_val->level)
                ->select('username')
                ->get()->toArray();
            $calculate = self::get_calculate($user_count_val->level);
            $amount = $base * $calculate /100/$user_count_val->count;
            foreach($user as $user_val){

                $user_insert_arr[] = [
                    'token' => 'FIL',
                    'username' => $user_val->username,
                    'amount' => $amount,
                    'ticket' => 'data_Red_' . date('YmdHis'),
                    'type' => 'profit',
                    'type_note' => '分红',
                    'state' => 1,
                ];

            }
        }
        if(count($user_insert_arr)){
            Db::table('user_balance')->insert($user_insert_arr);
        }

    }


    public static function get_calculate($level){
        $config_level = Cache::get('config_level');

        $level_arr = array_column($config_level,'level');
        if(in_array($level,$level_arr)){
            $key = array_keys($level_arr,$level)[0];
        }else{
            array_push($level_arr,$level);
            sort($level_arr);
            $key = array_keys($level_arr,$level)[0]-1;
        }
        $calculate = $config_level[$key]->calculate;
        return $calculate;
    }
}





